<?php
class CategoryController extends Controller {

    public $defaultAction = 'index';
    
	public function actionIndex() {
        $this->pageTitle = Yii::t('app', 'Категория');
        
        $st_id = intval(CHttpRequest::getParam('st_id'));
        $rubric_id = intval(CHttpRequest::getParam('rid'));
        $category_id = intval(CHttpRequest::getParam('cid'));
        
        $rubrics = Yii::app()->basestatic->rubrics($st_id);
        $rubric = isset($rubrics[$rubric_id]) ? $rubrics[$rubric_id] : array();		
		$category = isset($rubric['categories'][$category_id]) ? $rubric['categories'][$category_id] : array();
		
		
        $criteria = new CDbCriteria();
        $criteria->compare('t.hidden', 0);
		//$criteria->compare('t.deleted', 0);
        $criteria->compare('t.active', 1);
        $criteria->compare('service_type', $st_id);
		$criteria->compare('rubric_id', $rubric_id);
		$criteria->compare('category_id',  $category_id);
		$criteria->compare('t.country_id', Yii::app()->params['default_country_id']);
        if (0 < Yii::app()->session['region_id']) {
			$criteria->compare('t.region_id', intval(Yii::app()->session['region_id']));
		}
        if (0 < Yii::app()->session['city_id']) {
			$criteria->compare('t.city_id', intval(Yii::app()->session['city_id']));
		}
        $criteria->order = 't.rating_order DESC';
        
        $count = Firms::model()->count($criteria);
        
        $pages = new CPagination($count);
        $pages->pageSize = Yii::app()->params['list_firms_perpage'];
        $page_param = CHttpRequest::getParam('page');

        if ('all' != $page_param) {
            $pages->applyLimit($criteria);
        }		
        
        $firms = Firms::model()->with('region', 'city')->findAll($criteria);
        //$firms = Firms::model()->findAll($criteria);

		$this->render('index', array(
            'firms' => $firms,
            'pages' => $pages,
			'url_viewAll' => $this->createViewAllUrl(),
			'url_viewPager' => $this->createViewPagerUrl(),
			'page_param' => $page_param,
			'st_id' => $st_id,
			'rubric_id' => $rubric_id,
			'category_id' => $category_id,
			'rubric' => $rubric,
			'category' => $category,
			
        ));
	}
    
	public function createViewAllUrl() {
		$params = $_GET;
		$params['page']='all';
		return $this->createUrl($this->route,$params);
	}
	
	public function createViewPagerUrl() {
		$params = $_GET;
		unset($params['page']);
		return $this->createUrl($this->route,$params);
	}    
  
}

?>